package com.sbr.seb.wicket.util;

import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.planning.Month;
import com.sbr.seb.entity.planning.Week;

public class PlanningUtil {
	private static final Logger logger = LoggerFactory.getLogger(PlanningUtil.class);
	private static final String[] days = {" ","L","M","M","J","V"," "};

	private static void initCalendar(Calendar calendar, Month month) {
		calendar.set(Calendar.MONTH, month.getNumber());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setMinimalDaysInFirstWeek(1);
	}

	public static List<Week> createWeekList(List<Week> weekList, Month month) {
		Calendar calendar = Calendar.getInstance();
		initCalendar(calendar, month);
		logger.debug("maximum number of week in month "+calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));
		int numberOfWeekInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
		weekList.clear();
		for (int j = 1;j <= numberOfWeekInMonth;j++) {
			Week week = new Week();

			logger.debug("day of week "+calendar.get(Calendar.DAY_OF_WEEK));
			if (j == 1) {
				if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
					week.setNumberOfDay(9 - calendar.get(Calendar.DAY_OF_WEEK));
					logger.debug("Day is not Sunday ("+calendar.get(Calendar.DAY_OF_WEEK)+")");
				} else {
					week.setNumberOfDay(1);
					logger.debug("Day is Sunday");
				}
			} else if (j == numberOfWeekInMonth) {
				logger.debug("last week of the month");
				calendar.set(Calendar.MONTH, month.getNumber());
				calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
				logger.debug(calendar.getTime().toString());
//				week.setNumberOfDay(calendar.get(Calendar.DAY_OF_WEEK));
				if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
					week.setNumberOfDay(calendar.get(Calendar.DAY_OF_WEEK) - 1);
					logger.debug("Day is not Sunday ("+calendar.get(Calendar.DAY_OF_WEEK)+")");
				} else {
					week.setNumberOfDay(7);
					logger.debug("Day is Sunday");
				}
			} else {
				week.setNumberOfDay(7);
				logger.debug("setting the number of day at 7");
			}

			week.setNumber(calendar.get(Calendar.WEEK_OF_YEAR)); // on travaille sur les semaine du mois mais on veut le number de la semaine dans l'année
			calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR)+1);
			logger.debug(week.toString());
			weekList.add(week);
		}
		return weekList;
	}

	public static List<String> createDayNames(List<String> dayNames, Month month) {
		Calendar calendar = Calendar.getInstance();
		initCalendar(calendar,month);
		int i = 0;
		dayNames.clear();
		for (int j = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);j <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH);j++) {
			calendar.set(Calendar.DAY_OF_MONTH, j);
			logger.debug("add day number "+days[calendar.get(Calendar.DAY_OF_WEEK)-1]);
			dayNames.add(days[calendar.get(Calendar.DAY_OF_WEEK)-1]);
			i++;
		}
		return dayNames;
	}

	public static List<Integer> createDayNumber(List<Integer> dayNumbers, Month month) {
		Calendar calendar = Calendar.getInstance();
		initCalendar(calendar,month);
		logger.debug("maximum de jour "+calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		dayNumbers.clear();
		//		dayNumbers = new ArrayList<Integer>(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		int i = 0;
		for (int j = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);j <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH);j++) {
			calendar.set(Calendar.DAY_OF_MONTH, j);
			logger.debug("add day number "+calendar.get(Calendar.DAY_OF_MONTH));
			dayNumbers.add(calendar.get(Calendar.DAY_OF_MONTH));
			i++;
		}

		logger.debug("longueur du dayNumbers "+dayNumbers.size());
		return dayNumbers;
	}
}
